package com.infisight.hudprojector.kdxfspeech;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.util.ResourceUtil;
import com.iflytek.cloud.util.ResourceUtil.RESOURCE_TYPE;

/**
 * @author hao 语音合成
 * 
 */
public class SpeechVoiceCompound {

	public String TAG = "SpeechVoiceCompound";

	public static SpeechVoiceCompound svcManager;
	// 语音合成对象
	private SpeechSynthesizer mTts;
	// 默认云端发音人
	public static String voicerCloud = "xiaoyan";
	// 默认本地发音人
	public static String voicerLocal = "xiaoyan";
	Context mContext;
	// 引擎类型SpeechConstant(公共常量)
	private String mEngineType = SpeechConstant.TYPE_LOCAL;

	// 语音识别对象
	SpeechRecognizer mIat = null;
	RecognizerListener recL;
	SpeechVoiceRecognition voiceSvr;
	AudioManager audioManager;
	int max;

	public SpeechVoiceCompound(Context context, String str,
			SpeechVoiceRecognition svr) {
		audioManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 获取系统音乐媒体最大音量
		if (svr != null) {
			mIat = svr.mIat;
			voiceSvr = svr;
		}
		mContext = context;

		if (null == mTts) {
			// 初始化合成对象
			mTts = SpeechSynthesizer.createSynthesizer(mContext,
					mTtsInitListener);
			setParam();
		}
		startSpeaking(str);
	}

	public static SpeechVoiceCompound getInstance(Context context, String str,
			SpeechVoiceRecognition svr) {
		if (svcManager == null) {
			svcManager = new SpeechVoiceCompound(context, str, svr);
		}
		return svcManager;
	}

	public void startSpeaking(String str) {
		if (str != null) {
			mTts.startSpeaking(str, mTtsListener);
		}
	}

	public void stopSpeaking() {
		if (mTts != null)
			mTts.stopSpeaking();
	}

	/**
	 * 初始化监听。 语音+初始化完成回调接口。
	 */
	private InitListener mTtsInitListener = new InitListener() {
		@Override
		public void onInit(int code) {
			Log.d("compound", "InitListener init() code = " + code);
			if (code != ErrorCode.SUCCESS) {
				// showTip("初始化失败,错误码："+code);
			}
		}
	};
	/**
	 * 合成回调监听。
	 */
	private SynthesizerListener mTtsListener = new SynthesizerListener() {

		@Override
		public void onSpeakBegin() {
			// showTip("开始播放");
			audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, max / 5, 0);
			stopListen();
		}

		@Override
		public void onSpeakPaused() {
			// showTip("暂停播放");
		}

		@Override
		public void onSpeakResumed() {
			// showTip("继续播放");
		}

		@Override
		public void onBufferProgress(int percent, int beginPos, int endPos,
				String info) {
			// mPercentForBuffering = percent;
			// mToast.setText(String.format(getString(R.string.tts_toast_format),
			// mPercentForBuffering, mPercentForPlaying));
			//
			// mToast.show();
		}

		@Override
		public void onSpeakProgress(int percent, int beginPos, int endPos) {
			// mPercentForPlaying = percent;
			// showTip(String.format(getString(R.string.tts_toast_format),
			// mPercentForBuffering, mPercentForPlaying));
		}

		@Override
		public void onCompleted(SpeechError error) {
			audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, max / 2, 0);
			if (error == null) {
				// showTip("播放完成");

				mTts.stopSpeaking();
			} else if (error != null) {
				// showTip(error.getPlainDescription(true));
			}
			Log.i(TAG, "onCompleted");
			startListen();
		}

		@Override
		public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
			// TODO Auto-generated method stub

		}
	};

	// 这里是在语音合成中,控制语音识别结束
	public void stopListen() {
		if (mIat != null) {
			mIat.stopListening();
		}
	}

	// 这里是在语音合成中,控制语音识别开始
	public void startListen() {
		voiceSvr.init(mContext);
	}

	public void destroy() {
		if (mIat != null) {
			mIat.destroy();
		}
		if (mTts != null) {
			mTts.destroy();
		}
	}

	/**
	 * 参数设置
	 * 
	 * @param param
	 * @return
	 */
	private void setParam() {
		mTts.setParameter(SpeechConstant.PARAMS, null);
		// 设置合成
		if (mEngineType.equals(SpeechConstant.TYPE_CLOUD)) {
			// 设置使用云端引擎
			mTts.setParameter(SpeechConstant.ENGINE_TYPE,
					SpeechConstant.TYPE_CLOUD);
			// 设置发音人
			mTts.setParameter(SpeechConstant.VOICE_NAME, voicerCloud);
		} else {
			// 设置使用本地引擎
			mTts.setParameter(SpeechConstant.ENGINE_TYPE,
					SpeechConstant.TYPE_LOCAL);
			// 设置发音人资源路径
			mTts.setParameter(ResourceUtil.TTS_RES_PATH, getResourcePath());
			// 设置发音人
			mTts.setParameter(SpeechConstant.VOICE_NAME, voicerLocal);
		}

		// 设置语速
		mTts.setParameter(SpeechConstant.SPEED, "50");

		// 设置音调
		mTts.setParameter(SpeechConstant.PITCH, "50");
		// mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "false");

		// 设置音量80
		mTts.setParameter(SpeechConstant.VOLUME, "5");
		// 设置音量
		mTts.setParameter(SpeechConstant.VOLUME, "20");

		// 设置播放器音频流类型
		mTts.setParameter(SpeechConstant.STREAM_TYPE, "4");

	}

	// 获取发音人资源路径
	private String getResourcePath() {
		StringBuffer tempBuffer = new StringBuffer();
		// 合成通用资源
		tempBuffer.append(ResourceUtil.generateResourcePath(mContext,
				RESOURCE_TYPE.assets, "tts/common.jet"));
		tempBuffer.append(";");
		// 发音人资源
		tempBuffer.append(ResourceUtil.generateResourcePath(mContext,
				RESOURCE_TYPE.assets, "tts/" + SpeechVoiceCompound.voicerLocal
						+ ".jet"));
		Log.i("compound", tempBuffer.toString());
		return tempBuffer.toString();
	}

}
